package com.example.hyznbk.presenter;

import android.util.Log;

import com.example.hyznbk.model.api.ImpAndDifTutorApi;
import com.example.hyznbk.model.result.DifficultReviewEntity;
import com.example.hyznbk.presenter.presenter_interface.IDifficultReviewPresenter;
import com.example.hyznbk.view.base.BaseFragment;
import com.example.hyznbk.view.base.ILoadMoreDataView;
import com.example.hyznbk.view.fragment.DifficultReviewFragment;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import java.util.List;

import testPaperNDFX.DifficultReviewPaperActivity;

/**
 * Created by tangminglong on 17/7/29.
 */

public class DifficultReviewPresenter implements IDifficultReviewPresenter {

    private ILoadMoreDataView view;

    private int status = -1;//题型的状态
    private String queType = "";//默认所有题型
    private int pagIndex = 1;

    private int lastPosition = 0;

    private ProgressSubscriber mSubscriber;


    private HttpOnNextListener<DifficultReviewEntity> listener = new HttpOnNextListener<DifficultReviewEntity>() {
        @Override
        public void onNext(DifficultReviewEntity impAndDifTutorEntity) {
            if (impAndDifTutorEntity.getTotalCount() > 0) {
                view.hideLoading();
                List<DifficultReviewEntity.ImpAndDifTutorBean> modelList = impAndDifTutorEntity.getModelList();
                for (int i = 0 ; i < modelList.size() ; i ++){//预处理题库返回不合理的地方
                    String summary = modelList.get(i).getQuesBrief();
                   /* summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/

                    String regexStr = "<[^>]*>"; //去除所有的标签
                    summary = summary.replaceAll(regexStr, "");
                    if (summary.length() > 100){
                        summary = summary.substring(0,100);
                    }

                    modelList.get(i).setQuesBrief(summary);
                }

                if (pagIndex == 1) {
                    view.loadData(impAndDifTutorEntity);
                    lastPosition = 0;
                }else {
                    view.loadMoreData(impAndDifTutorEntity);
                }

            }else {
                view.showNoData("暂无难点复习内容");
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.showNetError();
        }
    };

    public DifficultReviewPresenter(ILoadMoreDataView view) {
        this.view = view;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        ImpAndDifTutorApi api = new ImpAndDifTutorApi(listener,(DifficultReviewFragment)view);
        api.setPageIndex(pagIndex);
        api.setStatus(status);
        api.setQueType(queType);
        api.setPageSize(10+lastPosition);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) view.showLoading();
        pagIndex = 1;
        getData();

    }

    @Override
    public void getMoreData() {
        pagIndex = pagIndex +1;
        getData();
    }

    @Override
    public void getDataByStatus(int quesStatusId) {
        if (status == quesStatusId) return;
        status = quesStatusId;
        getData(false);
    }

    @Override
    public void getDataByQuesType(String quesTypeId) {
        if (queType.equals(quesTypeId)) return;
        queType = quesTypeId;
        getData(false);
    }

    public void openItemDetail(int position, DifficultReviewEntity.ImpAndDifTutorBean impAndDifTutorBean) {
        lastPosition = position;
        UserInfo.PaperStatus = impAndDifTutorBean.getState();
        UserInfo.PaperFrom = impAndDifTutorBean.getQuesFrom();
        //TODO 调用下面方法进行跳转
//        PaperInfor.ModelFrom = 0;//普通联系
        DifficultReviewPaperActivity.launch((BaseFragment) view,impAndDifTutorBean, PaperDef.FROM_DIFFERENT_REVIEW);
        Log.e("========QuesID======",""+impAndDifTutorBean.getQuesID());
    }
}
